<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
      <BasicForm @register="registerForm">
                 <!-- <template #buildSlot="{ model, field }">
                <a-select
                    v-model:value="model[field]" @change="buildChange"
                    style="width: 180px" :fieldNames="{label:'buildName',value:'id' }"
                    :options="buildOptions"
                >
                </a-select>
        </template>
        <template #storeySlot="{ model, field }">
                <a-select
                    v-model:value="model[field]" @change="storeyChange"
                    style="width: 180px" :fieldNames="{label:'storeyName',value:'id' }"
                    :options="storeyOptions"
                >
                </a-select>
        </template> -->
        <template #roomSlot="{ model, field }">
                <a-select
                    v-model:value="model[field]" @change="roomChange" disabled
                    style="width: 180px" :fieldNames="{label:'roomCode',value:'id' }"
                    :options="roomOptions"
                >
                </a-select>
        </template>
        <template #bedSlot="{ model, field }">
                <a-select disabled
                    v-model:value="model[field]" 
                    style="width: 180px" :fieldNames="{label:'bedCode',value:'id' }"
                    :options="bedOptions"
                >
                </a-select>
        </template>
      </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema} from '../DormBedPerson.data';
    import {saveOrUpdate,buildList,storeyList,roomList,bedList} from '../DormBedPerson.api';
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    let buildOptions= ref([])
    let storeyOptions= ref([])
    let roomOptions= ref([])
    let bedOptions= ref([])
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        await getBuildOptions()
        await getStoreyOptions()
        await getRoomOptions()
        await getBedOptions()
        if (unref(isUpdate)) {
            console.log(data.record);
            data.record.bedId = data.record.bedId_dictText
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
        }else{
          await setFieldsValue({
                ...data.record,
            });
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    async function buildChange(e){
       await setFieldsValue({
               storeyId:'',
               roomCode: '',
               bedId: ''
        })
        roomOptions.value = []
        getStoreyOptions(e)
    }
    async function storeyChange(e){
        await setFieldsValue({
               roomCode: '',
               bedId: ''
        })
      getRoomOptions(e)
    }
    async function roomChange(e){
         await setFieldsValue({
               bedId: ''
        })
      getBedOptions(e)
    }
    function getBuildOptions(){
        return buildList({
          status:1,
          pageSize:100,
        }).then(res=>{
          buildOptions.value = res.records
        })
    }
    function getStoreyOptions(e){
      return storeyList({
        buildId:e? e : '',
        status:1,
        pageSize:100,
      }).then(res=>{
        storeyOptions.value = res.records
      })
    }
    function getRoomOptions(e){
      return roomList({
        storeyId: e ? e : '',
        status:1,
        pageSize:100,
      }).then(res=>{
        roomOptions.value = res.records

      })
    }
    function getBedOptions(e){
      return bedList({
        roomId: e ? e : '',
        status:1,
        pageSize:100,
      }).then(res=>{
        bedOptions.value = res.records
        console.log(bedOptions.value);
      })
    }
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>